package com.snail.billing.session;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import com.snail.billing.DataCache;
import com.snail.billing.net.BillingArrayHttpSession;
import com.snail.billing.net.BillingEncode;
import com.snail.billing.net.BillingSecurity;

public class WorkQuerySession extends BillingArrayHttpSession {

	private int type;

	private int start;

	public WorkQuerySession(int type, int start) {
		DataCache cache = DataCache.getInstance();
		String host = cache.importParams.hostBusiness;
		// host = "172.18.70.142";
		String address = String.format("http://%s/problem/queryWorkForm.do", host);
		setAddress(address);
		this.type = type;
		this.start = start;
		addBillingPair("userName", BillingEncode.enCode(cache.importParams.account, "UTF-8"));
		addBillingPair("aid", "");
		addBillingPair("workformId", "");
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT);
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -1);
		addBillingPair("beginTime", format.format(calendar.getTime()));
		addBillingPair("endTime", "");
		addBillingPair("start", String.valueOf(start));
		addBillingPair("limit", "10");
		String extparams = "";
		if (type == 0) {
			String state = "a,1,2,3,4,5,6,7,8,9";
			extparams = String.format("JSON:{\"gameId\":%s,\"state\":\"%s\"}", cache.importParams.gameId, state);
		} else if (type == 1) {
			String state = "a,1,2,3,4,5,8,9";
			extparams = String.format("JSON:{\"gameId\":%s,\"state\":\"%s\"}", cache.importParams.gameId, state);
		} else if (type == 2) {
			String state = "6,7";
			extparams = String.format("JSON:{\"gameId\":%s,\"state\":\"%s\"}", cache.importParams.gameId, state);
		} else {
			extparams = String.format("JSON:{\"gameId\":%s}", cache.importParams.gameId);
		}
		addBillingPair("extparams", extparams);
		buildParamPair();
	}

	@Override
	public BillingSecurity getSecurity() {
		if (DataCache.getInstance().isSandbox) {
			return BillingSecurity.securitySandbox2;
		}
		return BillingSecurity.security;
	}

	public int getType() {
		return type;
	}

	public int getStart() {
		return start;
	}

}
